package com.lib.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.lib.common.constant.CreateUpdateConstant;
import com.lib.common.constant.StatusConstant;
import com.lib.mapper.CategoryMapper;
import com.lib.mapper.LibraryMapper;
import com.lib.pojo.DTO.BookInformationDTO;
import com.lib.pojo.DTO.CategoryDTO;
import com.lib.pojo.DTO.PageDTO;
import com.lib.pojo.VO.BookInformationVO;
import com.lib.pojo.VO.CategoryVO;
import com.lib.pojo.entity.BookInformation;
import com.lib.pojo.entity.PageResult;
import com.lib.service.LibraryService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class LibraryServiceImpl implements LibraryService {

    @Autowired
    private LibraryMapper libraryMapper;
    @Autowired
    private CategoryMapper categoryMapper;

    //1：新增图书
    @Override
    @Transactional
    public void addLibrary(BookInformationDTO bookInformationdto) {
        //属性拷贝
        BookInformation bookInformation = new BookInformation();
        BeanUtils.copyProperties(bookInformationdto,bookInformation);

        //添加时间和创建人
        bookInformation.setCreateTime(LocalDateTime.now());
        bookInformation.setUpdateTime(LocalDateTime.now());
        bookInformation.setCreateUser(CreateUpdateConstant.CREATE_USER);
        bookInformation.setUpdateUser(CreateUpdateConstant.UPDATE_USER);
        //默认图书是下架的
//        bookInformation.setStatus(StatusConstant.STATUS_DOWN);
        //添加一个图书
        libraryMapper.addLibrary(bookInformation);
        //把书的id赋给DTO
        Integer bookId = bookInformation.getBookId();
        bookInformationdto.setBookId(bookId);

        //2：把这本书属于的分类添加
        libraryMapper.putCategory(bookInformationdto);
    }

    //2：根据id查询套餐
    @Override
    public BookInformationVO getById(Integer id) {
        //获取当前id对于的分类集合
        List<CategoryDTO> categoryDTO = libraryMapper.getCategoryIdAndName(id);
        //获取当前di对应的图书
        BookInformation bookInformation = libraryMapper.getById(id);
        //创建返回前端的数据
        BookInformationVO bookInformationVO = new BookInformationVO();
        //把分类集合赋给VO
        bookInformationVO.setList(categoryDTO);
        //把图书赋给VO
        BeanUtils.copyProperties(bookInformation,bookInformationVO);
        return bookInformationVO;
    }

    //3：根据分类id查询菜品
    @Override
    public List<CategoryVO> getByCategoryId(Integer categoryId) {
        return categoryMapper.getCategoryById(categoryId);
//        return libraryMapper.getByCategoryId(categoryId);
    }

    //4：图书的起售、停售
    @Override
    public void statusStartOrStop(Integer status, Integer id) {
        //修改图书的状态
        libraryMapper.statusStartOrStop(status,id);
    }

    //5:批量删除
    @Override
    @Transactional
    public void delete(List<Integer> ids) {
        //1:判读这个图书是否在起售中
        for (Integer id : ids){
            BookInformation bookInformation = libraryMapper.getById(id);
            if (bookInformation.getStatus() == StatusConstant.STATUS_UP){
                throw new RuntimeException("图书正在售卖中，不能删除");
            }
        }

        //2：判断这个图书是否关联的有套餐
        List<Integer> TaoCanId = libraryMapper.UnifyId(ids);
        if (TaoCanId != null && TaoCanId.size()>0){
            //当前要删除的图书关联了套餐不能被删除
            throw new RuntimeException("当前图书关联了套餐，不能删除");
        }

        //3：批量删除图书
        for (Integer id : ids){
            libraryMapper.deleteGuanLianById(id);
        }
        libraryMapper.delete(ids);

    }

    //6:修改图书
    @Transactional(rollbackFor = Exception.class)
    @Override
    public void putBook(BookInformationVO bookInformationVO) {
        //先删除与图书有观的分类
        libraryMapper.deleteGuanLianById(bookInformationVO.getBookId());
        //先把list也就是分类集合从VO中取出来，在重新添加
        List<CategoryDTO> list = bookInformationVO.getList();
        //创建用来添加分类关联的DTO，并传入图书的ID
        BookInformationDTO bookInformationDTO = new BookInformationDTO();
        bookInformationDTO.setBookId(bookInformationVO.getBookId());
        for (CategoryDTO categoryDTO : list) {
            //把分类id赋给DTO
            bookInformationDTO.setCategoryId(categoryDTO.getCategoryId());
            //把分类添加到数据库
            libraryMapper.putCategory(bookInformationDTO);
        }
        BeanUtils.copyProperties(bookInformationVO,bookInformationDTO);
        //使用build构建bookInformationDTO
//        bookInformationDTO = BookInformationDTO.builder()
//                .bookId(bookInformationVO.getBookId())
//                .sort(bookInformationVO.getSort())
//                .description(bookInformationVO.getDescription())
//                .image(bookInformationVO.getImage())
//                .bookName(bookInformationVO.getBookName())
//                .author(bookInformationVO.getAuthor())
//                .place(bookInformationVO.getPlace())
//                .build();
        //把剩余的进行修改
        libraryMapper.putBook(bookInformationDTO);
    }

    //7：图书分页查询
    @Override
    public PageResult page(PageDTO pageDTO) {
        PageHelper.startPage(pageDTO.getPage(),pageDTO.getPageSize());
        if (pageDTO.getCategoryId() != null){
            //只查询指定分类下的图书
            Page<BookInformation> page = libraryMapper.page(pageDTO);
            long total = page.getTotal();
            List<BookInformation> records = page.getResult();
            return new PageResult(total,records);
        }
        //查询所有
        Page<BookInformation> page = libraryMapper.pageAll(pageDTO);
        long total = page.getTotal();
        List<BookInformation> records = page.getResult();
        return new PageResult(total,records);
    }

    //8：根据id删除图书
    @Override
    public void deleteById(Integer id) {
        //先把图书的关联id删除掉
        libraryMapper.deleteGuanLianById(id);
        libraryMapper.deleteRecommendById(id);
        libraryMapper.deleteById(id);
    }

    //9：将指定图书添加到套餐
    @Override
    public void addDishAndSetmeal(Integer bookId, Integer recommendId) {
        libraryMapper.addDishAndSetmeal(bookId,recommendId);
    }

}
